define(['./BaseView', '../models/ownParticular', '../libs/extend', 'underscore'], function (BaseView, ownParticular, extend, _) {
    return BaseView.extend({
        themeData: null,
        dataNum:null,
        index_this:null,
        events: {
            "click .Delate": "Delate",
            "click .chooseYes": "chooseYes",
            "click .my-change": "myChange",
            "click .addCancle": "addCancle",
            "click .Revise": "Revise",
            "click .my-data": "myItem"
        },
        //初始化
        initialize: function () {
            dataNum = 2;
            var mysurvey = '<div class="my-title">我的调研记录</div><div class="mysurvey"><ul class="mydatalist"></ul></div>';
            this.$el.append(mysurvey);
            var template = library.getTemplate('alertChange.html');
            this.$el.append(template);
            this.themeData = new ownParticular();
            this.listenTo(this.themeData, 'reset', this.onRest);

        },
        //重新加载数据
        onRest: function () {
            var data = this.themeData.toJSON();
            this.createMain(data);
            var that = this;
            //监听滚动条事件
            var switchs = false;
            $('.mysurvey').bind({
                touchstart:function(e){
                    var touch = e.originalEvent.targetTouches[0];//获取第一个触点
                    var y = Number(touch.pageY); //页面触点Y坐标
                    //记录触点初始位置
                    startY = y;
                },
                touchmove: function (e) {
                    // e.preventDefault();
                    var touch = e.originalEvent.targetTouches[0]; //获取第一个触点
                    var y = Number(touch.pageY); //页面触点Y坐标
                    var scrolltop = $(this).scrollTop();
                    if(scrolltop<=0){
                        if (y - startY >= 30) {
                            $('.mydatalist').css('transform','translateY(30px)');
                        } else if(y - startY>=0 && y - startY<30){
                            var lengthY =  y - startY ;
                            $('.mydatalist').css('transform','translateY('+lengthY+'px)');
                            $('.pulldown').css('transform','translateY('+lengthY+'px)');
                            switchs = true;
                        }
                    }
                }
            });
            $('.mysurvey').bind("touchcancel touchend",function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.mydatalist').outerHeight();
                var userId = extend.getlocalStorage("YY_userInfo");
                $('.pulldown').css('transform','translateY(0px)');
                that.scroll();
                if (scrolltop >= offsetH - height - 50 && scrolltop != 0) {
                    $('.mysurvey').unbind('scroll');
                    $('.pullup').hide();
                    that.$el.find('.loading').show();
                    indexData = {
                        "userId":userId.userId,
                        "pageindex":dataNum,
                        "pagesize":10
                    };
                    extend.sendData(window.app.proxy.root + 'getResearchRecordByUser', indexData, function(res){
                        if(res.status == 'success'){
                            if (res.data.length == 0) {
                                that.$el.find('.loading').hide();
                                that.$el.find('.noMore').show();
                                setTimeout(
                                    function () {
                                        that.$el.find('.noMore').hide();
                                    }, 300
                                );
                                return false
                            } else {
                                setTimeout(function () {
                                    that.createMain(res.data);
                                    dataNum++;
                                }, 300)
                            }
                        }else{
                            extend.Toast(res.message,2000);
                        }
                    },function(e){
                        extend.Toast('网络错误',2000);
                    });
                }
                if(switchs == true){
                    var indexData = {
                        "userId":userId.userId,
                        "pageindex":1,
                        "pagesize":10
                    };
                    $('.addCancle').hide();
                    $('.my-change').show() ;
                    extend.sendData(window.app.proxy.root + 'getResearchRecordByUser', indexData, function (res) {
                        if (res.status == 'success') {
                            if (res.data.length !== 0) {
                                that.$el.find('.loadingTop').show();
                                setTimeout(
                                    function () {
                                        $('.mydatalist').css('transform','translateY(0px)').html('');
                                        that.$el.find('.loadingTop').hide();
                                        that.createMain(res.data);
                                    }, 300
                                );
                                return false
                            }else{
                                extend.Toast('暂无数据', 2000);
                            }


                        } else {
                            extend.Toast(res.message, 2000);
                        }
                    }, function (e) {
                        extend.Toast('网络错误', 2000);
                    });
                    switchs = false;
                }
            });
        },
        scroll:function(){
            $('.mysurvey').bind("scroll",function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.mydatalist').outerHeight();
                if (scrolltop >= offsetH - height) {
                    $('.pullup').show();
                } else {
                    $('.pullup').hide();
                }
            })
        },

        //创建列表
        createMain: function (data) {
            var len = data.length;
            if (len) {
                for (var i = 0; i < len; i++) {
                    var item = data[i];
                    var itemId = item.pk_record;
                    var Username = item.username; //人員姓名
                    var project = item.project; //項目
                    var begindate = item.begindate;//起始時間
                    if (item) {
                        var listItemDom = [
                                '<li class="my-item" data-id=' + itemId + '>',
                                '<div class="my-data" data-id=' + itemId + '>',
                                '<p class="project">' + project + '</p>',
                                '<p class="username"><span>负责人：</span><span>' + Username + '</span></p>',
                                '<span class="begindate">' + begindate + '</span>',
                                '</div>',
                                '<div class="Revise">修改</div>',
                                '<div class="Delate">删除</div>',
                                '</li>'

                            ].join(''),
                            $listItemDom = $(listItemDom);
                        this.$el.find('.mydatalist').append($listItemDom);

                    }
                }
                this.$el.find('.loading').hide();
            }else{
                extend.Toast('暂无记录',2000);
            }
        },
        //点击删除按钮弹出提示框
        Delate:function(e){
            $('.alertChange').show();
            index_this = $('.Delate').index(e.currentTarget);
            //点击取消删除按钮
            $('.chooseNo').bind('click', function () {
                $('.alertChange').hide()
            });
        },
        //点击确定删除按钮
        chooseYes:function(e){
            $('.alertChange').hide();
            var itemId = $('.my-item').eq(index_this).attr('data-id');
            var data = {
                "pk_record":itemId
            };
            extend.sendData(window.app.proxy.root + 'deleteResearchRecord', data, function(res){

                if(res.status == 'success'){
                    $('.my-item').eq(index_this).remove();
                }else{
                    extend.Toast(res.message,2000);
                }
            },function(e){
                extend.Toast('网络错误',2000);
            });
        },
        //编辑按钮
        myChange:function(){
            $('.Delate').show();
            $('.Revise').show();
            $('.my-change').hide();
            $('.addCancle').show()
        },
        //取消编辑
        addCancle:function(){
            $('.Delate').hide();
            $('.Revise').hide();
            $('.addCancle').hide();
            $('.my-change').show()
        },
        //进入修改页面
        Revise:function(e){
            $('.Delate').hide();
            $('.Revise').hide();
            $('.addCancle').hide();
            $('.my-change').show()
            index_this = $('.Revise').index(e.currentTarget);
            var itemId = $('.my-item').eq(index_this).attr('data-id');
            extend.setlocalStorage('YY_themsID',itemId);
            Backbone.history.navigate('#myDataAmend', {trigger: true});
        },
        myItem:function(e){
            var dataid = e.currentTarget.getAttribute("data-id");
            //存在本地数据库
            extend.setlocalStorage('YY_themsID', dataid);
            Backbone.history.navigate('#Particular', {
                trigger: true
            });
        }
    });
});

